AddCSLuaFile( "cl_init.lua" ) // Make sure clientside  
AddCSLuaFile( "shared.lua" )  // and shared scripts are sent.     
include('shared.lua')     

--setting up
function ENT:Initialize()  
	util.PrecacheSound("common/wpn_hudoff.wav")
	util.PrecacheSound("items/ammo_pickup.wav")
	util.PrecacheSound("common/null.wav")
	self.Entity:SetModel( "models/props_c17/oildrum001.mdl")  	
--	self.Entity:SetModel( "models/props_junk/metalgascan.mdl")  
	self.Entity:PhysicsInit( SOLID_VPHYSICS )      // Make us work with physics,  	
	self.Entity:SetMoveType( MOVETYPE_VPHYSICS )   // after all, gmod is a physics  	
	self.Entity:SetSolid( SOLID_VPHYSICS )         // Toolbox                
	local phys = self.Entity:GetPhysicsObject()  	
	if (phys:IsValid()) then  		
		phys:Wake()  	
	end  
	
	if ( RES_DISTRIB == 1 ) then
		RD_AddResource(self.Entity, "energy", 5000)
		RD_AddResource(self.Entity, "air", 5000)
		RD_AddResource(self.Entity, "coolant", 5000)
		RD_AddResource(self.Entity, "hydrogen", 5000)
	end
	
	--don't touch any of this crap
	self.used = false
	self.pickedup = false
	self.player = nil
	self.touched = false
	self.val1 = 0
	self.val2 = 0
	self.val3 = 0
	self.val4 = 0
	
end     

--spawnit!
function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end

	local SpawnPos = tr.HitPos + tr.HitNormal * 16

	local ent = ents.Create( "sent_backpack_lifesupport" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
end


--for getting things in and out of, currently you can do this while it's on someones back. Pickpocketing anyone?
function ENT:Use(activator)
end
 
 --stops use firing a million times
function unuse(self)
self.used = false
end
 
 
 --Put things into it with the physics gun, or pick it up by walking into it
function ENT:Touch(activator)
	if (!self.touched) then
		if (activator:IsPlayer()) then
			local a = ents.FindByClass("sent_backpack_lifesupport")
			local found = false
			local n = nil
			for k,v in ipairs(a) do
				if (v.player == activator) then
					found = true
				end
			end
			if (!found) then --don't let the bastards have more than one pack
				self.Entity:SetOwner(activator)
				--self.Entity:GetPhysicsObject():EnableCollisions(false) 
				local a = activator:GetAngles()
				local k = activator:GetAimVector()
				local f = k:Angle()
				f.p = a.p
				f.r = a.r
				local d = f:Forward()
				local r = activator:GetPos()
				r.z = r.z + 31
				a.p = a.p + 8
				r = r + d * -13
				self.Entity:SetPos(r)
				self.Entity:SetAngles(a)
				self.Entity:SetGravity(0)
				self.Entity:SetParent(activator)
				self.player = activator
				self.pickedup = true
				self.Entity:EmitSound( "items/ammo_pickup.wav", 500, 100 )
			else
				self.Entity:EmitSound( "common/null.wav", 500, 100 )
			end
			self.touched = true
			timer.Simple(0.3,untouch,self) 
		end
	end
end

--stops the damn touch function firing a million times
function untouch(self)
self.touched = false
end

--if the fool's dead or disconnected have him drop it. Or he can drop it on his own by crouching
--From what I can work out, disconnecting destroys the pack and anything inside it :/
function ENT:Think()
	self.val1 = RD_GetResourceAmount(self.Entity, "air")
	self.val3 = RD_GetResourceAmount(self.Entity, "coolant")
	self.val2 = RD_GetResourceAmount(self.Entity, "energy")
	self.val4 = RD_GetResourceAmount(self.Entity, "hydrogen")
	self:SetOverlayText( "Air: " .. self.val1 .. "\nEnergy: " .. self.val2 .. "\nCoolant: " .. self.val3 .. "\nHydrogen: " .. self.val4)
	if (self.pickedup) then
		if (self.player:KeyDown(IN_DUCK) ) or (!self.player:Alive()) then
			self.Entity:SetOwner()
			self.Entity:SetGravity(1)
			self.Entity:SetParent()
			--	self.Entity:GetPhysicsObject():EnableCollisions(true) 
			local a = self.player:GetAngles()
			local d = self.player:GetForward()
			local r = self.player:GetPos()
			r.z = r.z + 20
			r = r + d * -50
			self.Entity:SetPos(r)
			self.Entity:SetAngles(a)
			self.pickedup = false
			self.player = nil
			self.used = true
			timer.Simple(0.3,unuse,self)
			self.touched = true
			timer.Simple(0.3,untouch,self)
			self.Entity:GetPhysicsObject():Wake() 
			self.Entity:EmitSound( "common/wpn_hudoff.wav", 500, 100 )
		end
	end
end

function ENT:OnRemove()
	if (RES_DISTRIB == 1) then Dev_Unlink_All(self.Entity) end
end
